package com.yycwxt.modules.admin.user;

import com.yycwxt.common.ReturnResult;
import com.yycwxt.domain.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;


@Api
@RestController
@Validated
@RequestMapping("/user")
public class UserController {

    private final RedisTemplate<Object, Object> redisTemplate;


    private final UserService userService;

    public UserController(@Autowired UserService userService, RedisTemplate redisTemplate){
        this.userService = userService;
        this.redisTemplate = redisTemplate;
    }



    @PostMapping("/save")
    @ApiOperation("保存修改的用户信息")
    public ReturnResult save(@RequestParam @NotNull(message = "不能为空") @NotBlank(message = "长度不能为0")  @ApiParam(name = "code账号" ,required = true, type = "string")  String code,
                             @RequestParam @NotNull(message = "不能为空") @NotBlank(message = "长度不能为0") @ApiParam(name = "username用户名" ,required = true, type = "string")  String username,
                             @RequestParam @ApiParam(name = "type账号类型" ,required = true, type = "string") String type,
                             @RequestParam @NotNull(message = "不能为空")  @ApiParam(name = "id" ,required = true, type = "int") Integer id){
        return ReturnResult.buildSuccessResult(userService.update(code, username, type, id));
    }

    @PostMapping("/find")
    @ApiOperation("保存登录的用户信息")
    public User find(@Validated  HttpServletRequest request){
        return (User) request.getSession().getAttribute("user");
//        return (User) redisTemplate.opsForValue().get("user");
    }

    @PostMapping("/login")
    @ApiOperation("查询登录用户信息")
    public ReturnResult findByCode(@RequestParam @NotNull(message = "不能为空") @NotBlank(message = "长度不能为0") @ApiParam(name = "账号", required = true, type = "String") String code,
                                   @RequestParam @NotNull(message = "不能为空") @NotBlank(message = "长度不能为0") @ApiParam(name = "密码", required = true, type = "String") String password, HttpServletRequest request){
        // System.out.println(code + "," + password);

        User user = userService.findByCode(code, password);
//        redisTemplate.opsForValue().set("user", user);
        request.getSession().setAttribute("user", user);
        request.getSession().setMaxInactiveInterval(12 * 60 * 60);
        return ReturnResult.buildSuccessResult(user);
    }

    @PostMapping("/update")
    @ApiOperation("修改密码")
    public ReturnResult updatePassword(@Validated  @ApiParam(name = "code账号" ,required = true, type = "string")  String code, @ApiParam(name = "username用户名" ,required = true, type = "string")  String username, @ApiParam(name = "password密码" ,required = true, type = "string")  String password){
        return ReturnResult.buildSuccessResult(userService.updatePassword(code, username, password));
    }
}
